package com.server.adapter;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumAdapter extends AbstractJobmineAdapter {

	@Override
	/*
	 * gets HTML source from the interview page on Jobmine
	 *
	 * @param id User id
	 * @param pwd User password
	 */
	public String getHTML(String id, String pwd) {
		String src = null;
		WebDriver driver = null;

		try {
			/*
			 * spawn a new firefox browser process.
			 */
			driver = new FirefoxDriver();

			/*
			 * open jobmine webpage
			 */
			driver.get("https://jobmine.ccol.uwaterloo.ca/servlets/iclientservlet/SS/?cmd=login");

			/*
			 * input user name
			 */
			WebElement userId = driver.findElement(By.name("userid"));
			userId.sendKeys(id);

			/*
			 * input user password
			 */
			WebElement pw = driver.findElement(By.name("pwd"));
			pw.sendKeys(pwd);

			/*
			 * submit the form.
			 */
			WebElement submit = driver.findElement(By.name("submit"));
			submit.click();

			Thread.sleep(1000);

			/*
			 * Navigate to interviews page
			 */
			driver.switchTo().frame("main");

			WebElement use = driver.findElement(By.xpath("//tr[2]/td[2]/a/img"));
			use.click();

			Thread.sleep(1000);

			WebElement interviews = driver.findElement(By.xpath("//tr[12]/td[3]/a/u"));
			interviews.click();

			Thread.sleep(1000);

			/*
			 * Retrieve page source in HTML
			 */
			src = driver.getPageSource();

			/*
			 *  Close the browser
			 */
			driver.quit();
		} catch (Exception e) {
			System.out.println("Selenium Err: Login " + e.getMessage());
			
			driver.quit();
			return null;
		}

		return src;
	}

}